﻿@if (false) {<script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.7.min.js" type="text/javascript" />}
@{string list = "list18", pager = "pager18", entity = "ProduceOrder";}
<style type="text/css">
    #outrepository { width: 649px; border:0px solid; font-size: 12px; }
    #outrepository input { border: 0; text-align: right; width: 150px; }
    #orderitemtable { border-top: solid 1px rgb(170,170,170); border-left: solid 1px rgb(170,170,170); }
    #orderitemtable th { width: 80px; }
    #orderitemtable th, #orderitemtable td { padding:3px; border-bottom: solid 1px rgb(170,170,170); border-right: solid 1px rgb(170,170,170); }
    #orderitemtable input { border: 0px; width: 80px; }
    
    #outrepository1 { width: 649px; border:0px solid; font-size: 12px; }
    #orderitemtable1 { border-top: solid 1px rgb(170,170,170); border-left: solid 1px rgb(170,170,170); }
    #orderitemtable1 th { width: 80px; }
    #orderitemtable1 th, #orderitemtable1 td { padding:3px; border-bottom: solid 1px rgb(170,170,170); border-right: solid 1px rgb(170,170,170); }
</style>
<table id="@list" class="scroll" cellpadding="0" cellspacing="0">
</table>
<div id="@pager" class="scroll" style="text-align: center;">
</div>
<div id="ProduceOrderdig" title="添加生产单">
<div id="outrepository">
    <h1 style="text-align:center"><input id="configuration" type="text" style="font-size: 1em; width: 130px;" />产品领料（出库）单</h1><br />
    <div style="margin:10px">
        <label>生产批号：</label><input type="text" style="text-align: left; margin-right:100px;" id="producenumber" /><label>计划数量：</label><input id="plancount" type="text" value="0" style="width: 60px;" /><label>台</label>
    </div>
    <table id="orderitemtable" cellspacing="0" cellpadding="0" border="0" class="ui-jqgrid-btable">
        <tr>
            <th class="ui-state-default ui-th-column ui-th-ltr" style="width: 100px">物料编号</th>
            <th class="ui-state-default ui-th-column ui-th-ltr" style="width: 140px">物料名称</th>
            <th class="ui-state-default ui-th-column ui-th-ltr">规格</th>
            <th class="ui-state-default ui-th-column ui-th-ltr">型号</th>
            <th class="ui-state-default ui-th-column ui-th-ltr">单位</th>
            <th class="ui-state-default ui-th-column ui-th-ltr">单台用量</th>
            <th class="ui-state-default ui-th-column ui-th-ltr">应发数量</th>
            <th class="ui-state-default ui-th-column ui-th-ltr">实发数量</th>
            <th class="ui-state-default ui-th-column ui-th-ltr">出库日期</th>
            <th class="ui-state-default ui-th-column ui-th-ltr">批次号</th>
            <th class="ui-state-default ui-th-column ui-th-ltr">备注</th>
        </tr>
    </table>
</div>
</div>
<div id="printproductorder" title="打印生产单">
<div id="outrepository1">
    <h1 style="text-align:center"><label id="name" /></h1><br />
    <div style="margin:10px">
        <label>生产批号：</label><label style="margin-right:150px;" id="number" /><label>计划数量：</label><label id="count" /><label>台</label>
    </div>
    <table id="orderitemtable1" cellspacing="0" cellpadding="0" border="0" class="ui-jqgrid-btable">
        <tr>
            <th class="ui-state-default ui-th-column ui-th-ltr" style="width: 100px">物料编号</th>
            <th class="ui-state-default ui-th-column ui-th-ltr" style="width: 140px">物料名称</th>
            <th class="ui-state-default ui-th-column ui-th-ltr">规格</th>
            <th class="ui-state-default ui-th-column ui-th-ltr">型号</th>
            <th class="ui-state-default ui-th-column ui-th-ltr">单位</th>
            <th class="ui-state-default ui-th-column ui-th-ltr">单台用量</th>
            <th class="ui-state-default ui-th-column ui-th-ltr">应发数量</th>
            <th class="ui-state-default ui-th-column ui-th-ltr">实发数量</th>
            <th class="ui-state-default ui-th-column ui-th-ltr">出库日期</th>
            <th class="ui-state-default ui-th-column ui-th-ltr">批次号</th>
            <th class="ui-state-default ui-th-column ui-th-ltr">备注</th>
        </tr>
    </table>
    <div style="margin:10px">
        <label style="margin-right:250px;">领料人：</label>
        <label>仓库：</label>
    </div>
</div>
</div>
<script type="text/javascript">
    function clear() {
        $("#name").text("");
        $("#number").text("");
        $("#count").text("");

        $("#orderitemtable1 td").parent().remove();
    };
    function FloatMul(arg1, arg2) {
        var m = 0, s1 = arg1.toString(), s2 = arg2.toString();
        try { m += s1.split(".")[1].length } catch (e) { }
        try { m += s2.split(".")[1].length } catch (e) { }
        return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m)
    }
    $(function () {
        $("#configuration").click(function () {
            $(this).autocomplete("search", "");
        });
        $("#plancount").keyup(function () {
            $.each($("#orderitemtable tr"), function (index, value) {
                if (index == 0) return;
                var c = FloatMul($(this).find("td").eq(5).html(), $("#plancount").val());
                $(this).find("td").eq(6).html(c);
                $(this).find("td").eq(7).find("input").val(c);
            });
        });
        $("#configuration").autocomplete({
            source: "/Configuration/GetConfigurations",
            minLength: 0,
            select: function (event, ui) {
                $.getJSON("/Configuration/Items?id=" + ui.item.id, function (data) {
                    //清表数据
                    $("#orderitemtable td").parent().remove();

                    var d = new Date();
                    var m = d.getMonth() + 1;
                    var date = d.getFullYear() + '-' + m + '-' + d.getDate()
                    var items = [];
                    $.each(data, function (key, val) {
                        $.each(val, function (i, v) { if (v == null) { val[i] = ""; } });
                        var item = "<tr><td>" + val[0] + "</td>" +
                            "<td>" + val[1] + "</td>" +
                            "<td>" + val[2] + "</td>" +
                            "<td>" + val[3] + "</td>" +
                            "<td>" + val[4] + "</td>" +
                            "<td style='text-align: right'>" + val[5] + "</td>" +
                            '<td style="text-align: right">' + FloatMul(val[5], $("#plancount").val()) + '</td>' +
                            '<td><input type="text" value="0" /></td>' +
                            '<td><input type="text" class="date" value=' + date + ' /></td>' +
                            '<td></td><td></td>';
                        items.push(item);
                    });
                    $("#orderitemtable").append(items.join(''));
                    $('.date').datepicker();
                    $('.number').click(function () {
                        var number = $(this).parent().parent().find("td").eq(0).html();
                        $(this).autocomplete({
                            source: "/InDepository/InDepositorys?number=" + number,
                            minLength: 0
                        });
                        $(this).autocomplete("search", "");
                    });
                })
            }
        });

    })
    $(function () {
        $("#printproductorder").dialog({
            autoOpen: false,
            height: 580,
            width: 750,
            modal: true,
            buttons:
            {
                "打印": function () {
                    Print($("#outrepository1"), $("#printproductorder"))
                },
                "取消": function () {
                    $(this).dialog("close");
                }
            }
        });

        $("#ProduceOrderdig").dialog({
            autoOpen: false,
            height: 580,
            width: 750,
            modal: true,
            buttons:
            {
                "保存": function () {
                    var data = { "number": $("#producenumber").val(),
                        "name": $("#configuration").val() + "产品领料（出库）单",
                        "count": $("#plancount").val()
                    };
                    $.each($("#orderitemtable tr"), function (index, value) {
                        if (index == 0)
                            return;
                        var names = ["ProductValueObject.Number", "ProductValueObject.Name", "ProductValueObject.Spec", "ProductValueObject.Model", "ProductValueObject.Unit", "SingleCount", "ShouldCount", "RealCount", "Date", "Number", "Remark"]
                        $.each($(this).find("td"), function (i, val) {
                            var n = "outDepositories[" + (index - 1) + "]." + names[i];
                            if ($(this).find("input").length == 0) {
                                data[n] = $(this).html();
                            }
                            else {
                                data[n] = $(this).find("input").eq(0).val();
                            }
                        });
                    });

                    $.post("/Configuration/Out", data, function (value) {
                        if (value.success) {
                            Alert("恭喜", "出库成功！");
                            $("#ProduceOrderdig").dialog("close");
                            $("#@list").trigger("reloadGrid");
                        }
                        else {
                            Alert("错误", value.message);
                        }
                    });
                },
                "取消": function () {
                    $(this).dialog("close");
                }
            }
        });
        $("#@list").jqGrid({
            url: "/@entity/index/",
            colNames: ["生产批号", "配置单名称", "日期", "数量", "入库数量", "备注"],
            colModel: [$.jgridx.strCol("Number"), $.jgridx.strCol("Name"), $.jgridx.dateCol("Date"), $.extend($.jgridx.intCol("Count"),
            {
                editable: false
            }), $.extend($.jgridx.intCol("InDepositoryCount"),
            {
                editable: false
            }),
            {
                name: "Remark",
                index: "Remark",
                width: 280,
                editable: true,
                edittype: "textarea",
                search: false
            }],

            pager: $('#@pager'),
            sortname: "@(entity)Id",
            sortorder: "desc",
            subGrid: true,
            subGridUrl: '/ProduceOrder/OutDepositoryindex',
            subGridModel: [{
                name: ["物料编号", "物料名称", "规格", "型号", "单位", "单台用量", "应发数量", "实发数量", "出库日期", "批次号", "备注"],
                width: [80, 80, 80, 80, 80, 80, 80, 80, 80, 80]
            }],
            loadComplete: function (data) {

                if (!data.success)
                    $(this).jqGrid("clearGridData", true);
            }
        });
        $("#@list").jqGrid('navGrid', '#@pager',
        {
            edit: true,
            del: false,
            add: false,
            search: true,
            refresh: true
        },
        {
            url: "/@entity/edit/"
        },
        {
            url: "/@entity/add/"
        },
        {
            url: "/@entity/delete/"
        }, {}, {}).navSeparatorAdd("@pager",
        {
            sepclass: "ui-separator",
            sepcontent: ''
        }).jqGrid('navButtonAdd', "@pager",
        {
            caption: "",
            buttonicon: "ui-icon ui-icon-plus",
            position: "last",
            title: "配置出库",
            onClickButton: function () {
                $("#ProduceOrderdig").dialog("open");
            }
        }).jqGrid('navButtonAdd', "@pager",
        {
            caption: "",
            buttonicon: "ui-icon ui-icon-print",
            position: "last",
            title: "打印出库单",
            onClickButton: function () {
                clear();
                var rowid = $("#@list").jqGrid('getGridParam', 'selrow');
                if (rowid.length == 0) {
                    Alert("警告", "请选择记录！");
                    return;
                }
                $.getJSON("/ProduceOrder/GetProduceOrder?id=" + rowid, function (data) {
                    if (data.success) {
                        $("#name").html(data.name);
                        $("#number").html(data.number);
                        $("#count").html(data.count);

                        its = [];
                        $.each(data.outDepositories, function (index, value) {
                            its2 = [];
                            $.each(value, function (i, val) {

                                if (val == null) val = "";
                                if (i == 7 || i == 5 || i == 6 || i == 8) {
                                    its2.push("<td style='text-align: right'>" + val + "</td>");
                                } else {
                                    its2.push("<td>" + val + "</td>");
                                }
                            });
                            its.push("<tr>" + its2.join('') + "</tr>");
                        });
                        $(its.join('')).appendTo($("#orderitemtable1"));

                        $("#printproductorder").dialog("open");
                    } else {
                        Alert("警告", data.message)
                    }
                });
            }
        });
    });
</script>